package com.ljdemo.lrecorder.utils;

import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Looper;
import android.view.Gravity;
import android.widget.Toast;

/**
 * 弹窗工具
 * <br/>
 * 修改日期 : 2023/3/23
 * */
public class AlertUtil {

    public static void alert(Context context,
                             String title,
                             String msg,
                             DialogInterface.OnClickListener okFun,
                             DialogInterface.OnClickListener escFun){
        AlertDialog.Builder builder=new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setPositiveButton("确定", okFun);
        builder.setNegativeButton("取消",escFun);
        builder.show();
    }

    public static void alert(Context context,
                             String title,
                             String msg,
                             String okText,
                             String escText,
                             DialogInterface.OnClickListener okFun,
                             DialogInterface.OnClickListener escFun){
        AlertDialog.Builder builder=new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setPositiveButton(okText, okFun);
        builder.setNegativeButton(escText,escFun);
        builder.show();
    }

    public static void alert(Context context,
                             String title,
                             String msg,
                             DialogInterface.OnClickListener okFun){
        AlertDialog.Builder builder=new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setPositiveButton("确定", okFun);
        builder.show();
    }

    public static void alert(Context context,
                             String title,
                             String msg){
        AlertDialog.Builder builder=new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
//        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                dialog.dismiss();
//            }
//        });
        builder.show();
    }

    /**
     * 显示上下居中的消息提示
     * */
    public static void showCenterToast(Context context,String message){
        Toast toast=Toast.makeText(context,message,Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.show();
    }

    /**
     * 异步显示上下居中的消息提示
     * */
    public static void showCenterToastAsync(Context context,String message){
        Looper.prepare();
        Toast toast=Toast.makeText(context,message,Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.show();
        Looper.loop();
    }

    /**
     * 创建通知栏通知
     * @param context 上下文
     * @param title 通知标题
     * @param text 通知文本
     * @param icon 图标，例：R.mipmap.ic_launcher
     * @param cls 点击通知后启动的activity
     * */
    public static void createNotification(Context context,String title,String text,int icon,Class<?> cls){
        Notification.Builder builder=new Notification.Builder(context);
        builder.setSmallIcon(icon) //设置图标
                .setContentTitle(title)
                .setContentText(text);

        // 设置点击后启动的activity
        Intent intent=new Intent(context,cls);
        PendingIntent pendingIntent=PendingIntent.getActivity(context,0,intent,0);
        builder.setContentIntent(pendingIntent);

        // 设置通知的响应操作
        builder.setAutoCancel(true);

        // 抄自 ScreenRecordService2 129行 -------------------------------------------------------------------
        //普通notification适配
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            builder.setChannelId("notification_id");
        }
        //前台服务notification适配
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationManager notificationManager=(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
            NotificationChannel channel=new NotificationChannel("notification_id","notification_name",NotificationManager.IMPORTANCE_LOW);
            notificationManager.createNotificationChannel(channel);
        }
        // 抄自 ScreenRecordService2 129行 -------------------------------------------------------------------

        // 发送通知到系统通知栏
        NotificationManager notificationManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify((int)  System.currentTimeMillis(),builder.build());

    }

}
